﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Lion.WeiXin.Core.Data
{
   public class VipCardDAO :IVipCard
    {
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int AID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_VipCard");
            strSql.Append(" where AID=@AID");
            SqlParameter[] parameters = {
					new SqlParameter("@AID", SqlDbType.Int,4)
			};
            parameters[0].Value = AID;

            return SqlHelper.Exists(strSql.ToString(), parameters);
        }

        public bool Exists(string cardCode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_VipCard");
            strSql.Append(" where Code=@Code");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,50)
			};
            parameters[0].Value = cardCode;

            return SqlHelper.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Models.VipCardModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_VipCard(");
            strSql.Append("Code,Dic_CardType,Dic_CardLevel,Password,Balance,Total,Recharge,Phone,RealName,Dic_State,CreateDate,EndDate,Creator,Last_Update_Date,Last_Changed_By,Remark,Could_Buy,Birthday)");
            strSql.Append(" values (");
            strSql.Append("@Code,@Dic_CardType,@Dic_CardLevel,@Password,@Balance,@Total,@Recharge,@Phone,@RealName,@Dic_State,@CreateDate,@EndDate,@Creator,@Last_Update_Date,@Last_Changed_By,@Remark,@Could_Buy,@Birthday)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,50),
					new SqlParameter("@Dic_CardType", SqlDbType.VarChar,12),
					new SqlParameter("@Dic_CardLevel", SqlDbType.VarChar,12),
					new SqlParameter("@Password", SqlDbType.VarChar,50),
					new SqlParameter("@Balance", SqlDbType.Decimal,9),
					new SqlParameter("@Total", SqlDbType.Decimal,9),
					new SqlParameter("@Recharge", SqlDbType.Decimal,9),
					new SqlParameter("@Phone", SqlDbType.VarChar,20),
					new SqlParameter("@RealName", SqlDbType.NVarChar,20),
					new SqlParameter("@Dic_State", SqlDbType.VarChar,12),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@EndDate", SqlDbType.DateTime),
					new SqlParameter("@Creator", SqlDbType.VarChar,50),
					new SqlParameter("@Last_Update_Date", SqlDbType.DateTime),
					new SqlParameter("@Last_Changed_By", SqlDbType.VarChar,50),
					new SqlParameter("@Remark", SqlDbType.NVarChar,100),
                    new SqlParameter("@Could_Buy", SqlDbType.VarChar,50),
                    new SqlParameter("@Birthday", SqlDbType.DateTime)};
            parameters[0].Value = model.Code;
            parameters[1].Value = model.Dic_CardType;
            parameters[2].Value = model.Dic_CardLevel;
            parameters[3].Value = model.Password;
            parameters[4].Value = model.Balance;
            parameters[5].Value = model.Total;
            parameters[6].Value = model.Recharge;
            parameters[7].Value = model.Phone;
            parameters[8].Value = model.RealName;
            parameters[9].Value = model.Dic_State;
            parameters[10].Value = model.CreateDate;
            parameters[11].Value = model.EndDate;
            parameters[12].Value = model.Creator;
            parameters[13].Value = model.Last_Update_Date;
            parameters[14].Value = model.Last_Changed_By;
            parameters[15].Value = model.Remark;
            parameters[16].Value = model.Could_Buy;
            parameters[17].Value = model.Birthday;

            object obj = SqlHelper.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Models.VipCardModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_VipCard set ");
            strSql.Append("Code=@Code,");
            strSql.Append("Dic_CardType=@Dic_CardType,");
            strSql.Append("Dic_CardLevel=@Dic_CardLevel,");
            strSql.Append("Password=@Password,");
            strSql.Append("Balance=@Balance,");
            strSql.Append("Total=@Total,");
            strSql.Append("Recharge=@Recharge,");
            strSql.Append("Phone=@Phone,");
            strSql.Append("RealName=@RealName,");
            strSql.Append("Dic_State=@Dic_State,");
            strSql.Append("CreateDate=@CreateDate,");
            strSql.Append("EndDate=@EndDate,");
            strSql.Append("Creator=@Creator,");
            strSql.Append("Last_Update_Date=@Last_Update_Date,");
            strSql.Append("Last_Changed_By=@Last_Changed_By,");
            strSql.Append("Remark=@Remark,");
            strSql.Append("Could_Buy=@Could_Buy,");
            strSql.Append("Birthday=@Birthday");
            strSql.Append(" where AID=@AID");
            SqlParameter[] parameters = {
                    new SqlParameter("@Code", SqlDbType.VarChar,50),
					new SqlParameter("@Dic_CardType", SqlDbType.VarChar,12),
					new SqlParameter("@Dic_CardLevel", SqlDbType.VarChar,12),
					new SqlParameter("@Password", SqlDbType.VarChar,50),
					new SqlParameter("@Balance", SqlDbType.Decimal,9),
					new SqlParameter("@Total", SqlDbType.Decimal,9),
					new SqlParameter("@Recharge", SqlDbType.Decimal,9),
					new SqlParameter("@Phone", SqlDbType.VarChar,20),
					new SqlParameter("@RealName", SqlDbType.NVarChar,20),
					new SqlParameter("@Dic_State", SqlDbType.VarChar,12),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@EndDate", SqlDbType.DateTime),
					new SqlParameter("@Creator", SqlDbType.VarChar,50),
					new SqlParameter("@Last_Update_Date", SqlDbType.DateTime),
					new SqlParameter("@Last_Changed_By",SqlDbType.VarChar,50),
					new SqlParameter("@Remark", SqlDbType.NVarChar,100),
                    new SqlParameter("@Could_Buy", SqlDbType.VarChar,50),
                    new SqlParameter("@Birthday", SqlDbType.DateTime),
					new SqlParameter("@AID", SqlDbType.Int,4)};
            parameters[0].Value = model.Code;
            parameters[1].Value = model.Dic_CardType;
            parameters[2].Value = model.Dic_CardLevel;
            parameters[3].Value = model.Password;
            parameters[4].Value = model.Balance;
            parameters[5].Value = model.Total;
            parameters[6].Value = model.Recharge;
            parameters[7].Value = model.Phone;
            parameters[8].Value = model.RealName;
            parameters[9].Value = model.Dic_State;
            parameters[10].Value = model.CreateDate;
            parameters[11].Value = model.EndDate;
            parameters[12].Value = model.Creator;
            parameters[13].Value = model.Last_Update_Date;
            parameters[14].Value = model.Last_Changed_By;
            parameters[15].Value = model.Remark;
            parameters[16].Value = model.Could_Buy;
            parameters[17].Value = model.Birthday;
            parameters[18].Value = model.AID;
           

            int rows = SqlHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int AID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_VipCard ");
            strSql.Append(" where AID=@AID");
            SqlParameter[] parameters = {
					new SqlParameter("@AID", SqlDbType.Int,4)
			};
            parameters[0].Value = AID;

            int rows = SqlHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string AIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_VipCard ");
            strSql.Append(" where AID in (" + AIDlist + ")  ");
            int rows = SqlHelper.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.VipCardModel GetModel(int AID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 AID,Code,Dic_CardType,Dic_CardLevel,Password,Balance,Total,Recharge,Phone,RealName,Dic_State,CreateDate,EndDate,Creator,Last_Update_Date,Last_Changed_By,Remark,Could_Buy,Birthday from T_VipCard ");
            strSql.Append(" where AID=@AID");
            SqlParameter[] parameters = {
					new SqlParameter("@AID", SqlDbType.Int,4)
			};
            parameters[0].Value = AID;

            Models.VipCardModel model = new Models.VipCardModel();
            DataSet ds = SqlHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return ConvertToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }
        public Models.VipCardModel GetModel(string cardCode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 AID,Code,Dic_CardType,Dic_CardLevel,Password,Balance,Total,Recharge,Phone,RealName,Dic_State,CreateDate,EndDate,Creator,Last_Update_Date,Last_Changed_By,Remark,Could_Buy,Birthday from T_VipCard ");
            strSql.Append(" where Code=@Code");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,50)
			};
            parameters[0].Value = cardCode;

            Models.VipCardModel model = new Models.VipCardModel();
            DataSet ds = SqlHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return ConvertToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.VipCardModel ConvertToModel(DataRow row)
        {
            Models.VipCardModel model = new Models.VipCardModel();
            if (row["AID"] != null && row["AID"].ToString() != "")
            {
                model.AID = int.Parse(row["AID"].ToString());
            }
            if (row["Code"] != null)
            {
                model.Code = row["Code"].ToString();
            }
            if (row["Dic_CardType"] != null)
            {
                model.Dic_CardType = row["Dic_CardType"].ToString();
            }
            if (row["Dic_CardLevel"] != null)
            {
                model.Dic_CardLevel = row["Dic_CardLevel"].ToString();
            }
            if (row["Password"] != null)
            {
                model.Password = row["Password"].ToString();
            }
            if (row["Balance"] != null && row["Balance"].ToString() != "")
            {
                model.Balance = decimal.Parse(row["Balance"].ToString());
            }
            if (row["Total"] != null && row["Total"].ToString() != "")
            {
                model.Total = decimal.Parse(row["Total"].ToString());
            }
            if (row["Recharge"] != null && row["Recharge"].ToString() != "")
            {
                model.Recharge = decimal.Parse(row["Recharge"].ToString());
            }
            if (row["Phone"] != null)
            {
                model.Phone = row["Phone"].ToString();
            }
            if (row["RealName"] != null)
            {
                model.RealName = row["RealName"].ToString();
            }
            if (row["Dic_State"] != null)
            {
                model.Dic_State = row["Dic_State"].ToString();
            }
            if (row["CreateDate"] != null && row["CreateDate"].ToString() != "")
            {
                model.CreateDate = DateTime.Parse(row["CreateDate"].ToString());
            }
            if (row["EndDate"] != null && row["EndDate"].ToString() != "")
            {
                model.EndDate = DateTime.Parse(row["EndDate"].ToString());
            }
            if (row["Creator"] != null)
            {
                model.Creator = row["Creator"].ToString();
            }
            if (row["Last_Update_Date"] != null && row["Last_Update_Date"].ToString() != "")
            {
                model.Last_Update_Date = DateTime.Parse(row["Last_Update_Date"].ToString());
            }
            if (row["Last_Changed_By"] != null && row["Last_Changed_By"].ToString() != "")
            {
                model.Last_Changed_By = row["Last_Changed_By"].ToString();
            }
            if (row["Remark"] != null)
            {
                model.Remark = row["Remark"].ToString();
            }
            if (row["Could_Buy"] != null)
            {
                model.Could_Buy = row["Could_Buy"].ToString();
            }
            if (row["Birthday"] != null && row["Birthday"].ToString() != "")
            {
                model.Birthday = DateTime.Parse(row["Birthday"].ToString());
            }
            //外键值
            if (row.Table.Columns.Contains("CardType"))
            {
                model.CardType = row["CardType"].ToString();
            }
            if (row.Table.Columns.Contains("State"))
            {
                model.State = row["State"].ToString();
            }
            if (row.Table.Columns.Contains("CardLevelName"))
            {
                model.CardLevelName = row["CardLevelName"].ToString();
            }

            return model;
        }
        protected List<Models.VipCardModel> ConvertToList(DataSet ds)
        {
            List<Models.VipCardModel> list = new List<Models.VipCardModel>();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                Models.VipCardModel model = ConvertToModel(row);
                list.Add(model);
            }
            return list;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Models.VipCardModel> GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select v.AID,Code,Dic_CardType,Dic_CardLevel,Password,Balance,Total,Recharge,Phone,RealName,Dic_State,v.CreateDate,EndDate,v.Creator,Last_Update_Date,v.Last_Changed_By,v.Remark,Could_Buy,Birthday,dt.dic_value as CardType,dl.dic_Value as CardLevelName, ds.dic_value as State");
            strSql.Append(" from T_VipCard v left join T_Dictionary dt on v.Dic_CardType= dt.Dic_Key left join T_Dictionary dl on v.Dic_CardLevel= dl.Dic_Key left join T_Dictionary ds on v.Dic_State= ds.Dic_Key");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            DataSet ds = SqlHelper.Query(strSql.ToString());
            return ConvertToList(ds);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public List<Models.VipCardModel> GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append("v.AID,Code,Dic_CardType,Dic_CardLevel,Password,Balance,Total,Recharge,Phone,RealName,Dic_State,v.CreateDate,EndDate,v.Creator,Last_Update_Date,v.Last_Changed_By,v.Remark,Could_Buy ,Birthday,dt.dic_value as CardType,dl.dic_Value as CardLevelName, ds.dic_value as State");
            strSql.Append(" from T_VipCard v left join T_Dictionary dt on v.Dic_CardType= dt.Dic_Key left join T_Dictionary dl on v.Dic_CardLevel= dl.Dic_Key left join T_Dictionary ds on v.Dic_State= ds.Dic_Key");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            DataSet ds = SqlHelper.Query(strSql.ToString());
            return ConvertToList(ds);
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM T_VipCard ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = SqlHelper.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public List<Models.VipCardModel> GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.AID desc");
            }
            strSql.Append(")AS Row, T.*  from T_VipCard T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            DataSet ds = SqlHelper.Query(strSql.ToString());
            return ConvertToList(ds);
        }


        public int ResetPassword(string cardCode, string password)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_VipCard set ");
            strSql.Append("Password=@Password,");
            strSql.Append("Last_Update_Date=@Last_Update_Date");
            strSql.Append(" where Code=@Code");
            SqlParameter[] parameters = {
					new SqlParameter("@Password", SqlDbType.VarChar,50),
					new SqlParameter("@Last_Update_Date", SqlDbType.DateTime),
					new SqlParameter("@Code", SqlDbType.VarChar,50)};
            parameters[0].Value = password;
            parameters[1].Value = DateTime.Now;
            parameters[2].Value = cardCode;

            int rows = SqlHelper.ExecuteSql(strSql.ToString(), parameters);
            return rows;
        }
    }
}
